package com.guli.product.web;

import com.guli.product.entity.CategoryEntity;
import com.guli.product.service.CategoryService;
import com.guli.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 打开页面时查询所有一级分类
     * @param model
     * @return
     */
    @GetMapping(value = {"/", "/index.html"})
    public String indexPage(Model model) {
        //查出所有的1级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Catgories();
        model.addAttribute("categories", categoryEntityList);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatelog() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogFromWithRedisson();
        return map;
    }
}
